import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('./views/index.vue'),
    children: [
      {
        path: '',
        redirect: '/home',
      },
      {
        path: '/home',
        name: 'home',
        component: () => import('./views/Home.vue'),
      },
      {
        path: '/order',
        name: 'order',
        component: () => import('./views/Order.vue'),
      },
      {
        path: '/me',
        name: 'me',
        component: () => import('./views/Me.vue'),
      },
      {
        path: '/address',
        name: 'address',
        component: () => import('./views/Address.vue'),
      },
      {
        path: '/city',
        name: 'city',
        component: () => import('./views/City.vue'),
      },
      {
        path: '/search',
        name: 'search',
        component: () => import('./views/search.vue'),
      },

      {
        path: '/shop',
        name: 'shop',
        redirect: '/goods',
        component: () => import('./views/shops/shop.vue'),
        children: [
          {
            path: '/evaluation',
            name: 'evaluation',
            component: () => import('./views/shops/evaluation.vue'),
          },
          {
            path: '/business',
            name: 'business',
            component: () => import('./views/shops/business.vue'),
          },
          {
            path: '/goods',
            name: 'goods',
            component: () => import('./views/shops/goods.vue'),
          },
        ],
      },
    ],
  },
  /* 登录 */
  {
    path: '/login',
    name: 'login',
    component: () => import('./views/login.vue'),
  },
  {
    path: '/addAddress',
    name: 'addAddress',
    component: () => import('./views/addAddress.vue'),
  },
  {
    path: '/myAddress',
    name: 'myAddress',
    component: () => import('./views/myAddress.vue'),
  },
  {
    path: '/Settlement',
    name: 'Settlement',
    component: () => import('./views/order/Settlement.vue'),
  },
  {
    path: '/pay',
    name: 'pay',
    component: () => import('./views/order/pay.vue'),
  },
  {
    path: '/orderInfo',
    name: 'orderInfo',
    component: () => import('./views/order/orderInfo.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'active',
})

/* 挂载导航守卫 */
// router.beforeEach((to, from, next) => {
//   const isLogin = localStorage.ele_login ? true : false
//   if (to.path === '/login') {
//     next()
//   } else {
//     isLogin ? next() : next('/login')
//   }
// })

/* 解决点击两次路由报错 */
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
